package com.xyhuo.leetcode.november;

/**
 * HTML 实体解析器
 * https://leetcode.cn/problems/html-entity-parser/
 *
 * @time 2023/11/23
 */
public class EntityParser {
//    public static void main(String[] args) {
//        EntityParser entityParser = new EntityParser();
////        String text = "&amp; is an HTML entity but &ambassador; is not.";
//        String text = "leetcode.com&frasl;problemset&frasl;all";
//        String s = entityParser.entityParser(text);
//        System.out.println(s);
//    }

    private boolean findChar(char[] targetChar, char[] charArray, int index, int length) {
        boolean find = true;
        for (int i1 = 0; i1 < length; i1++) {
            if (targetChar[i1] != charArray[index + i1]) {
                find = false;
                break;
            }
        }
        return find;
    }

    public String entityParser(String text) {
        char[] charArray = text.toCharArray();
        int length = charArray.length;
        char[] one = "&frasl;".toCharArray();
        char[] two = "&quot;".toCharArray();
        char[] three = "&apos;".toCharArray();
        char[] four = "&amp;".toCharArray();
        char[] five = "&gt;".toCharArray();
        char[] six = "&lt;".toCharArray();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            if (charArray[i] == '&') {
                if (i + 6 < length) {
                    boolean find = findChar(one, charArray, i, 7);
                    if (find) {
                        sb.append("/");
                        i += 6;
                        continue;
                    }
                }
                if (i + 5 < length) {
                    boolean findTwo = findChar(two, charArray, i, 6);
                    if (findTwo) {
                        sb.append("\"");
                        i += 5;
                        continue;
                    }
                    boolean findThree = findChar(three, charArray, i, 6);
                    if (findThree) {
                        sb.append("'");
                        i += 5;
                        continue;
                    }
                }
                if (i + 4 < length) {
                    boolean find = findChar(four, charArray, i, 5);
                    if (find) {
                        sb.append("&");
                        i += 4;
                        continue;
                    }
                }
                if (i + 3 < length) {
                    boolean findFive = findChar(five, charArray, i, 4);
                    if (findFive) {
                        sb.append(">");
                        i += 3;
                        continue;
                    }
                    boolean findSix = findChar(six, charArray, i, 4);
                    if (findSix) {
                        sb.append("<");
                        i += 3;
                        continue;
                    }
                }
            }
            sb.append(charArray[i]);
        }
        return sb.toString();

    }
}
